<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>05反转颜色</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			body {
				overflow: hidden;
			}
			#firCan {
				display: block;
				border: 1px solid khaki;
				margin: 20px auto;
			}
		</style>
	</head>
	<body>
		<canvas id="firCan" width="1000" height="625">
			您的浏览器版本过低, 不支持Canvas, 请升级!
		</canvas>
		<div style="text-align: center;">
			<input type="button" id="oneBtn" value="反转颜色" />
		</div>
	</body>
	<script type="text/javascript">
		// 1, 获取Canvas
		var firCan = document.getElementById("firCan");
		// 2, 获取 2d渲染上下文
		var context = firCan.getContext('2d');
		// 3, 加载图片
		// 创建 img对象
		var imgObj = new Image();
		// 设置 src
		imgObj.src = "img/banner3.png";
		// 图片加载完毕
		imgObj.onload = function() {
			// 图片绘制到 canvas: drawImage()
			context.drawImage(imgObj, 0, 0, 1280, 800, 0, 0, 1000, 625);
		}
		/*
		 * 反转颜色
		 */
		var oneBtn = document.getElementById("oneBtn");
		oneBtn.onclick = function() {
			// 获取像素值
			var imageData = context.getImageData(0, 0, firCan.width, firCan.height);
			// 修改像素值
			var pixels = imageData.data;
			for (var i = 0; i < imageData.width * imageData.height; i++) {
				pixels[i * 4] = 255 - pixels[i * 4];
				pixels[i * 4 + 1] = 255 - pixels[i * 4 + 1];
				pixels[i * 4 + 2] = 255 - pixels[i * 4 + 2];
			}
			// 展示像素
			context.putImageData(imageData, 0, 0);
		}
		
		
	</script>
</html>
